<?php

/*
 * A savoir : Si on veut remplir un input avec la valeur que l'on veut, avant de l'ajouter au formulaire,
 * on déclare une variables $_REQUEST['idInput'] et on créé l'input avec le paramètre $garderValeur à true.
 */
class Form
{
    // <editor-fold defaultstate="collapsed" desc="Champs">
    
        private $_id = "Inconnu";
        private $_action = "";
        private $_method = "Inconnue";    
        private $_texteTitre = "";
        private $_texteHTML = "";
        
    // </editor-fold>
        
    // <editor-fold defaultstate="collapsed" desc="Propriétés">
        
        public function __get($propriete) 
        {
            switch ($propriete) 
            {
                case "id" : return $this->_id;
                case "action" : return $this->_action;
                case "method" : return $this->_method;
                case "texteTitre" : return $this->_texteTitre;
                case "texteHTML" : return $this->_texteHTML;
            }
        }
        public function __set($propriete, $value) 
        {
            switch ($propriete) 
            {
                case "id" : $this->_id = $value; break;
                case "action" : $this->_action = $value; break;
                case "method" : $this->_method = $value; break;
                case "texteTitre" : $this->_texteTitre = $value; break;
                case "texteHTML" : $this->_texteHTML = $value; break;
            }
        }
        
    // </editor-fold>    
        
    // <editor-fold defaultstate="collapsed" desc="Constucteurs">
        public function Form($id, $action, $method, $texteTitre =  null, $lesOptions = null)
        {
            require_once("InputType.class.php");
            $this->id = $id;
            $this->action = $action;
            $this->method = $method;
            
            $divTitre = ($texteTitre==null)? "" : "<h2>$texteTitre</h2>";
            $htmlOptions = "";
            if ($lesOptions != null)
            {
                $htmlOptions = self::TraitementDesOptions($lesOptions);
            }
            $this->texteHTML = "<form id='$id' action='$action' method='$method' $htmlOptions>$divTitre";
        }  
    // </editor-fold>  
    
    // <editor-fold defaultstate="collapsed" desc="Méthodes Redéfinies">
        
        public function __toString() {
            return $this->texteHTML . "</form>";
        }
        
    // </editor-fold>  
        
    // <editor-fold defaultstate="collapsed" desc="Méthodes Création Formulaire">
        
        //Méthode qui va dans tout les ajouts de balises HTML traiter les options qui seront contenues dans un tableau à 2 colonnes imbriqué dans un autre
        private static function TraitementDesOptions($optionsInput)
        {
            $htmlOptions = "";
            foreach($optionsInput as $uneOptionInput)
            {
                $htmlOptions .= "$uneOptionInput[0]='$uneOptionInput[1]'";
            }
            return $htmlOptions;
        }
        
        private static function TraitementValeurPrecedente($id)
        {
            if (isset($_REQUEST[$id]))
            {
                return $_REQUEST[$id];
            }
            else
            {
                return '';                
            }
        }
        
        public function debutFieldSet($legende = null, $optionsFieldSet = null)
        {
            $htmlOptionsFieldSet = "";
            if ($optionsFieldSet != null)
            {
                $htmlOptionsFieldSet = Form::TraitementDesOptions($optionsFieldSet);
            }
            $htmlLegende = "";
            if (!empty($legende) && $legende != null)
            {
                $htmlLegende = "<legend>$legende</legend>";
            }
            $this->_texteHTML .= "<fieldset $htmlOptionsFieldSet>$htmlLegende";
        }
        
        public function finFieldSet()
        {
            $this->_texteHTML .= "</fieldset>";
        }
        
        public function addTextArea($id, $text, $rows, $cols, $garderValeur, $optionsInput = null)
        {
            //Si on a une valeur précédente récupérée avec un $_REQUEST, alors on le prend en priorité par rapport au texte par défaut définit.
            $value = '';
            if ($garderValeur == true)
            {
                $value = Form::TraitementValeurPrecedente($id);
            }
            else
            {
                $value = $text;
            }
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $this->texteHTML .= "<textarea name='$id' rows='$rows' cols='$cols'>$value</textarea>";
        }
        
        public function addInput($typeInput, $id, $label, $garderValeur, $optionsInput = null)
        {
            $value = '';
            if ($garderValeur == true)
            {
                $value = Form::TraitementValeurPrecedente($id);
            }
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            switch ($typeInput)
            {
                case InputType::TEXT:
                        $this->_texteHTML .= "<label for=$id $htmlOptionsInput >$label</label> <input type='text' name=$id id=$id value='$value' $htmlOptionsInput/>";
                        break;
                case InputType::PASSWORD:                    
                        $this->_texteHTML .= "<label for=$id $htmlOptionsInput >$label</label> <input type='password' name=$id id=$id value='$value' $htmlOptionsInput/>";
                        break;
                case InputType::HIDDEN:
                        $this->_texteHTML .= "<label for=$id $htmlOptionsInput >$label</label><input type='hidden' name=$id id=$id value='$value' $htmlOptionsInput />";
                        break;                    
                case InputType::SUBMIT:
                        $this->_texteHTML .= "<input type='submit' name=$id id=$id value=$label $htmlOptionsInput />";
                        break;                    
                case InputType::CHECKBOX:
                        $this->_texteHTML .= "<input name=$id id=$id type='checkbox' $htmlOptionsInput value='$value' /> <label for=$id $htmlOptionsInput>$label</label>";
                        break;
                case InputType::FILE:
                        $this->_texteHTML .= "<label for=$id $htmlOptionsInput >$label</label> <input name=$id type='file' $htmlOptionsInput value='$value' />";
                        break;
                case InputType::BUTTON:
                        $this->_texteHTML .= "<input type='button' value=$label name=$id id=$id $htmlOptionsInput />";
                        break;
            }
        }
        
        public function addRadio($name, $id, $valueOption, $isChecked, $garderValeur, $optionsInput = null)
        {
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $htmlChecked = "";
            if ($isChecked == true || ($garderValeur == true && Form::TraitementValeurPrecedente($name) == $valueOption ))
            {
                $htmlChecked = "checked=checked";
            }
            $this->_texteHTML .= "<label for=$valueOption $htmlOptionsInput> $valueOption </label> <input type='radio' name=$name value=$valueOption id=$id $htmlChecked $htmlOptionsInput/> ";
        }
        
        public function addSpan($contenu = null, $optionsInput = null)
        {
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $txtContenu = "";
            if ($contenu != null)
            {
                $txtContenu = $contenu;
            }
            $this->_texteHTML .= "<span $htmlOptionsInput>$txtContenu</span>";
        }
        
        public function debutSelect($id, $label, $options = null)
        {
            $htmlOptions = "";
            if ($options != null)
            {
                $htmlOptions = Form::TraitementDesOptions($options);
            }
            $this->_texteHTML .= "<label for=$id $htmlOptions>$label</label><select name=$id id=$id $htmlOptions>";
        }
        
        public function addOption($id, $value, $label, $garderValeur, $isSelected)
        {
            $htmlSelected = "";
            if ($isSelected || ($garderValeur == true && Form::TraitementValeurPrecedente($id) == $value ))
            {
                $htmlSelected = "selected";
            }
            $this->_texteHTML .= "<option $htmlSelected value=$value>$label</option>";
        }
        
        public function finSelect()
        {
            $this->_texteHTML .= "</select>";
        }
        
        public function debutDiv($optionsInput = null)
        {
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $this->_texteHTML .= "<div $htmlOptionsInput>";
        }
        
        public function finDiv()
        {
            $this->_texteHTML .= "</div>";
        }
        
        public function debutTable($optionsInput = null)
        {
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $this->_texteHTML .= "<table $htmlOptionsInput>";
        }
        
        public function finTable($optionsInput = null)
        {
            $this->_texteHTML .= "</table>";
        }
        
        public function debutTR($optionsInput = null)
        {
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $this->_texteHTML .= "<tr $htmlOptionsInput>";
        }
        
        public function finTR()
        {
            $this->_texteHTML .= "</tr>";
        }
        
        public function addTR($content, $optionsInput = null)
        {
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $this->_texteHTML .= "<tr $htmlOptionsInput>$content</tr>";
        }
        
        public function addTH($content, $optionsInput = null)
        {
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $this->_texteHTML .= "<th $htmlOptionsInput>$content</th>";
        }
        
        public function addTD($content, $optionsInput = null)
        {
            $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $this->_texteHTML .= "<td $htmlOptionsInput>$content</td>";
        }
        
        public function addLabel($id, $label, $optionsInput = null)
        {
           $htmlOptionsInput = "";
            if ($optionsInput != null)
            {
                $htmlOptionsInput = Form::TraitementDesOptions($optionsInput);
            }
            $this->_texteHTML .= "<label for='$id' $htmlOptionsInput>$label</label>"; 
        }
        
        public function addBR()
        {
            $this->_texteHTML .= "</br>";
        }
        
        public function addHTML($txthtml)
        {
            $this->_texteHTML .= $txthtml;
        }
    // </editor-fold>  
}